package com.yswee.mallnavi.model.info;

import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.mallnavi.context.ApiConstant;
import com.yswee.mallnavi.entity.ArrayData;
import com.yswee.mallnavi.entity.info.CategoryEntity;
import com.yswee.mallnavi.model.BaseModel;
import com.yswee.mallnavi.parser.BaseListGsonParser;
import com.yswee.mallnavi.parser.VoidParser;

import org.json.JSONArray;

public class CouponModel extends BaseModel<Void> {

    public CouponModel(ITaskContext context) {
        super(context);
    }

    @Override
    protected IParser<Void> createParser() {
        return new VoidParser();
    }

    public void getCategoryList(Callback<BaseArrayData<CategoryEntity>> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_COUPON_CATEGORY);
        getData(url, new CategoryParser(), c);
    }

    private class CategoryParser extends BaseListGsonParser<CategoryEntity> {
        @Override
        public BaseArrayData<CategoryEntity> parseData(String json) {
            try {
                mReponseData = json;
                JSONArray ja = asJSONArray(json);
                if (ja != null) {
                    mIsOk = true;
                    CategoryEntity t = null;
                    ArrayData<CategoryEntity> array = new ArrayData<CategoryEntity>();
                    for(int i = 0; i < ja.length(); i++) {
                        try {
                            t = parseDataT(ja.getJSONObject(i).toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if(t != null) {
                            array.array.add(t);
                        }
                    }
                    return array;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
